package com.netty.server.processor;

import com.netty.server.cmd.ByteCmd;
import com.netty.server.cmd.ByteResCmd;
import com.netty.server.cmd.LoginCmd;
import com.netty.server.processor.up.LoginPlugin;
import com.netty.server.processor.up.UpPlugin;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class DefaultUpProcessor implements UpProcessor {

    List<? extends UpPlugin<?>> upPlugins;
    private final LoginPlugin loginPlugin;

    public DefaultUpProcessor(LoginPlugin loginPlugin, List<? extends UpPlugin<?>> upPlugins) {
        this.upPlugins = upPlugins;
        this.loginPlugin = loginPlugin;
    }

    @Override
    public CompletableFuture<Boolean> login(LoginCmd loginCmd) {
        return CompletableFuture.completedFuture(loginPlugin.login(loginCmd));
    }

    @Override
    public void processor(ByteCmd byteCmd) {
        upPlugins.stream()
                .filter(x -> x.getCmd() == byteCmd.getCmdType())
                .findFirst()
                .orElseThrow(() -> new RuntimeException(String.format("[cmd = %s]未找到对应的上行处理插件", byteCmd.getCmdType())))
                .processor(byteCmd);
    }
}
